<?php
/**
 *
 */
namespace plugins\qbt_custom_model\service;
use think\Db;

class CtrlService{

    /**
     * 处理任务分发
     */
    public static function task($type,$field_info){
        return self::$type($field_info);
    }

    /**
     * 图片
     */
    private static function image($field_info){
        $str = '
        if (!empty($data[\''.$field_info['field_name'].'\'])) {
            $data[\''.$field_info['field_name'].'\'] = cmf_asset_relative_url($data[\''.$field_info['field_name'].'\']);
        }
        ';
        return $str;
    }

    /**
     * 多图片
     */
    private static function images($field_info){
        $html = 'if (!empty($data[\''.$field_info['field_name'].'_names\']) && !empty($data[\''.$field_info['field_name'].'_urls\'])) {
                $'.$field_info['field_name'].' = [];
                foreach ($data[\''.$field_info['field_name'].'_urls\'] as $key => $url) {
                    $'.$field_info['field_name'].'Url = cmf_asset_relative_url($url);
                    array_push($'.$field_info['field_name'].', ["url" => $'.$field_info['field_name'].'Url, "name" => $data[\''.$field_info['field_name'].'_names\'][$key]]);
                }
                unset($data["'.$field_info['field_name'].'_urls"]);
                unset($data["'.$field_info['field_name'].'_names"]);
                $data[\''.$field_info['field_name'].'\'] = json_encode($'.$field_info['field_name'].');
            }';
        return $html;
    }

    /**
     * 文件
     */
    private static function file($field_info){
        $str = '
        if (!empty($data[\''.$field_info['field_name'].'\'])) {
            $data[\''.$field_info['field_name'].'\'] = cmf_asset_relative_url($data[\''.$field_info['field_name'].'\']);
        }
        ';
        return $str;
    }

    /**
     * 多文件
     */
    private static function files($field_info){
        $str = '
        if (!empty($data[\''.$field_info['field_name'].'_names\']) && !empty($data[\''.$field_info['field_name'].'_urls\'])) {
                $'.$field_info['field_name'].' = [];
                foreach ($data[\''.$field_info['field_name'].'_urls\'] as $key => $url) {
                    $'.$field_info['field_name'].'Url = cmf_asset_relative_url($url);
                    array_push($'.$field_info['field_name'].', ["url" => $'.$field_info['field_name'].'Url, "name" => $data[\''.$field_info['field_name'].'_names\'][$key]]);
                }
                unset($data["'.$field_info['field_name'].'_urls"]);
                unset($data["'.$field_info['field_name'].'_names"]);
                $data[\''.$field_info['field_name'].'\'] = json_encode($'.$field_info['field_name'].');
            }
        ';
        return $str;
    }
    /**
     * 多选
     */
    private static function checkbox($field_info){
        $str = '
        if(isset($data["'.$field_info['field_name'].'"])&&count($data["'.$field_info['field_name'].'"])>0){
            $data["'.$field_info['field_name'].'"] = implode(",",$data["'.$field_info['field_name'].'"]);
        }
        ';
        return $str;
    }
    /**
     * 时间
     */
    private static function datetime($field_info){
        $str = '
            $data["'.$field_info['field_name'].'"] = strtotime($data["'.$field_info['field_name'].'"]);
        ';
        return $str;
    }
}